package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.InnerMarketDomain;
import com.itheima.domain.StockBlockDomain;
import com.itheima.domain.StockUpdownDomain;
import com.itheima.exception.BaseException;
import com.itheima.mapper.StockMapper;
import com.itheima.service.StockService;
import com.itheima.vo.resp.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {
    @Autowired
    StockMapper stockMapper;

    /**
     * 查询最新的国内大盘信息
     * @return
     */
    @Override
    public List<InnerMarketDomain> getInnerIndexAll() {
        List<InnerMarketDomain> domains = stockMapper.getInnerIndexAll();
        return domains;
    }

    /**
     * 板块指数
     * @return
     */
    @Override
    public List<StockBlockDomain> sectorAllLimit() {
        List<StockBlockDomain> domains = stockMapper.sectorAllLimit();
        return domains;
    }

    /**
     * 涨幅榜更多数据功能实现
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<StockUpdownDomain> stockAll(Integer page, Integer pageSize) {
        //1.获取最新的股票交易时间(假数据)
        LocalDateTime curDate = LocalDateTime.of(2021, 12, 30, 9, 32, 0);
        //2.设置分页参数
        PageHelper.startPage(page,pageSize);
        //3.调用mapper查询数据
        List<StockUpdownDomain> infos=stockMapper.getStockAll(curDate);
        //判断数据是否为空
        if (infos==null){
            throw new BaseException("数据不能为空");
        }
        //4.组装数据
        //转化成PageInfo对象
        PageInfo<StockUpdownDomain> pageInfo = new PageInfo<>(infos);
        //4.响应数据
        PageResult<StockUpdownDomain> pageResult = new PageResult<>(pageInfo);
        return pageResult;
    }

    /**
     * 涨幅榜功能
     * @return
     */
    @Override
    public List<StockUpdownDomain> increase() {
        LocalDateTime curDate = LocalDateTime.of(2021, 12, 30, 9, 32, 0);
        List<StockUpdownDomain> infos=stockMapper.getStock(curDate);
        return infos;
    }
}
